<template>
  <!--折叠面板 @ldy-->
  <div class="collapse">
    <div class="collapse-header">
      <div class="collapse-title" @click="clickHandle">
        <slot name="title">
          <i :class="['el-icon-arrow-down', 'transform', show ? '' : 'rotate']"></i>
          <span>{{title}}</span>
        </slot>
      </div>
      <div class="line"></div>
    </div>
    <div :class="[clickstate ? 'transform' : '']" :style="{height: (show ? height : '0px')}">
      <div class="collapse-body" v-show="show2">
        <slot>内容</slot>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    title: {
      type: String,
      default: '标题'
    }
  },
  watch: {
    show (val) {
      setTimeout(
        _ => {
          this.$nextTick(_ => {
            this.show2 = val
          })
        },
        val ? 0 : 500
      )
    }
  },
  data () {
    return {
      height: 0,
      clickstate: false,
      show2: true,
      show: true
    }
  },
  methods: {
    clickHandle () {
      this.clickstate = true
      this.$nextTick(_ => {
        this.show = !this.show
        setTimeout(_ => {
          this.clickstate = false
        }, 500)
      })
    }
  },
  updated () {
    this.height = this.$el.querySelector('.collapse-body').scrollHeight + 'px'
  },
  mounted () {
    this.height = (this.$el.scrollHeight - 40) + 'px'
  }
}
</script>

<style lang="scss" scoped>
.collapse {
  background-color: #fff;
  overflow: hidden;
  .collapse-header {
    display: flex;
    flex-direction: row;
    width: 100%;
    justify-content: space-between;
    justify-items: center;
    position: relative;
    .collapse-title {
      background-color: #fff;
      z-index: 2;
      padding-right: 15px;
      height: 40px;
      line-height: 40px;
      text-align: left;
      cursor: pointer;
      i {
        margin-right: 10px;
      }
      span {
        color: #0da9a0;
      }
      .rotate {
        transform: rotate(-180deg);
      }
    }
    .line {
      height: 1px;
      width: 100%;
      margin-top: 19px;
      background-color: #f1f2f3;
      position: absolute;
    }
  }
  .transform {
    transition: all 0.5s ease-in-out;
  }
}
</style>
